<template>
  <div class="login-container">
    <van-nav-bar class="page-nav-bar" title="登录" left-arrow @click-left="onClickLeft"></van-nav-bar>
    <van-form ref="loginForm" @submit="onSubmit">
      <van-field v-model="user.mobile" class="phoneField" name="mobile" placeholder="请输入手机号" :rules="formRules.mobile">
        <i slot="left-icon" class="iconfont icon-shouji1"></i></van-field>
      <van-field v-model="user.code" type="text" name="code" placeholder="请输入验证码" :rules="formRules.code">
        <i slot="left-icon" class="iconfont icon-yanzhengma"></i>
        <template #button>
          <van-count-down
            v-if="countDownShow"
            ref="countDown"
            class="countDown"
            :time="60000"
            :auto-start="true"
            format="mm:ss"
            @finish="countDownFinish"
          ></van-count-down>
          <van-button v-if="!countDownShow" round size="mini" class="send-msm-btn" native-type="button" @click="msmBtn">获取验证码</van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button block type="info" native-type="submit" class="submit-btn" @cilck="onSubmit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendMsm } from '@/api/user.js'
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data() {
    return {
      user: { mobile: '13811111111', code: '246810' },
      formRules: {
        mobile: [
          { required: true, message: '请填写手机号' },
          { pattern: /^1/, message: '请填写正确的手机号' }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          { pattern: /\d{6}/, message: '请填写正确的验证码' }
        ]
      },
      codePattern: /\d{6}/,
      // mobilePattern: /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/
      mobilePattern: /^1/,
      countDownShow: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onSubmit() {
      this.$toast.loading({
        duration: 0,
        forbidClick: true,
        message: '登陆中...'
      })
      try {
        await this.$refs.loginForm.validate()
        const res = await login(this.user)
        this.$toast.success('登陆成功')
        console.log(res);
        this.$store.commit('setUser', res.data.data)
        this.$router.push('/my')
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机号或验证码错误')
        } else {
          this.$toast.fail('登陆失败')
        }
      }
    },
    // 计时结束后
    countDownFinish() {
      this.countDownShow = false
    },
    async msmBtn() {
      await this.$refs.loginForm.validate('mobile')
      try {
        await sendMsm(this.user.mobile)
        this.$toast.success('发送成功')
        this.countDownShow = true
      } catch (err) {
        if (err.response.status === 404) {
          this.$toast.fail('手机号不正确')
        } else if (err.response.status === 429) {
          this.$toast.fail('发送太频繁了，休息一会儿吧')
        } else {
          this.$toast.fail('稍后再试')
        }
      }
    },
    onClickLeft() {
      this.$router.back()
    }
  }
}
</script>

<style scope lang="less">
.login-container {
  .van-nav-bar__arrow {
    color: #fff!important
  }
  .van-field__body {
    padding: 0 10px;
  }
  .icon {
    font-size: 37px;
  }
  .van-cell::after {
    border-bottom: 1px solid #999 !important;
  }
  .countDown {
    margin-right: 28px;
    color: #999;
  }
  .send-msm-btn {
    position: absolute;
    right: 20px;
    top: 3px;
    width: 152px;
    height: 46px;
    line-height: 46px;
    font-size: 22px !important;
    background-color: #ededed !important;
  }
  .van-button--default {
    color: #999 !important;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .submit-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
